<?php

namespace App\Observers;

use App\Models\Device;
use App\Models\VisitLog;
use Carbon\Carbon;

class VisitLogObserver
{
    /**
     * Handle the VisitLog "created" event.
     *
     * @param \App\Models\VisitLog $model
     * @return void
     */
    public function created(VisitLog $model)
    {
        $device = Device::query()->where(['device_id' => $model->device_id])->first();
        $device->visit_at = Carbon::now();
        $device->save();
    }

    /**
     * Handle the VisitLog "updated" event.
     *
     * @param \App\Models\VisitLog $model
     * @return void
     */
    public function updated(VisitLog $model)
    {
        //
    }

    /**
     * Handle the VisitLog "deleted" event.
     *
     * @param \App\Models\VisitLog $model
     * @return void
     */
    public function deleted(VisitLog $model)
    {
        //
    }

    /**
     * Handle the VisitLog "restored" event.
     *
     * @param \App\Models\VisitLog $model
     * @return void
     */
    public function restored(VisitLog $model)
    {
        //
    }

    /**
     * Handle the VisitLog "force deleted" event.
     *
     * @param \App\Models\VisitLog $model
     * @return void
     */
    public function forceDeleted(VisitLog $model)
    {
        //
    }

    public function creating(VisitLog $model)
    {

    }

    public function saving(VisitLog $model)
    {
        $device = Device::query()->where(['id' => $model->device_id])->first();
        $model->device_code = $device->code;

    }
}
